﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SlimDX;
using SlimDX.Direct3D11;
using ChaosEngine.GPU.Core;

namespace ChaosEngine.EngineCore.Graph.Logics
{
    public class MeshData : NodeLogic
    {
        /// <summary>
        /// NOTE - Editing this will NOT change actual mesh data. it is just an image of the vertex requested from the meshdata object.
        /// </summary>
        public struct VertexImage
        {

            public Vector3 Position;
            public Vector3 UV;
            public Vector3 Normal;
            public Vector3 Tangent;
            public Vector3 BiNormal;
            
        }

        public float[] Vertices;
        public float[] UV;
        public float[] AuxUV;
        public float[] Normals;
        public int[] Indices;

        public MeshData(int maxVertices,int maxTris):base("MeshData"){

            Vertices = new float[maxVertices * 3];
            UV = new float[maxVertices * 3];
            AuxUV = new float[maxVertices * 3];
            Normals = new float[maxVertices * 3 * 3];
            Indices = new int[maxTris*3];
            CacheBuffers = true;

        }

        public VertexImage GetVertex(int index)
        {

            VertexImage tmp = new VertexImage();
            
            int i = index * 3;

            tmp.Position = new Vector3(Vertices[i], Vertices[i + 1], Vertices[i + 2]);
            
            tmp.UV = new Vector3(UV[i], UV[i + 1], UV[i + 2]);
            
            i*=3;
        
            tmp.Normal = new Vector3(Normals[i], Normals[i + 1], Normals[i + 2]);
            tmp.Tangent = new Vector3(Normals[i + 3], Normals[i + 4], Normals[i + 5]);
            tmp.BiNormal = new Vector3(Normals[i + 6], Normals[i + 7], Normals[i + 8]);

            return tmp;
        }

        public void SetVertex(int index, VertexImage vi)
        {

            SetVertex(index, vi.Position, vi.UV, vi.Normal, vi.Tangent, vi.BiNormal);
        
        }



        public void SetVertex(int index, Vector3 pos, Vector3 uv, Vector3 norm, Vector3 tan, Vector3 bi)
        {

            int i = index * 3;

            Vertices[i] = pos.X;
            Vertices[i+ 1] = pos.Y;
            Vertices[i + 2] = pos.Z;

            UV[i] = uv.X;
            UV[i + 1] = uv.Y;
            UV[i + 2] = uv.Z;
            
            i*=3;
            
            Normals[i] = norm.X;
            Normals[i+ 1] = norm.Y;
            Normals[i + 2] = norm.Z;

            Normals[i + 3] = tan.X;
            Normals[i+4] = tan.Y;
            Normals[i+5] = tan.Z;

            Normals[i+6] = bi.X;
            Normals[i+7] = bi.Y;
            Normals[i+8] = bi.Z;


        }

        public bool CacheBuffers
        {
            get;
            set;

        }
        public vDataBuffer IndexBuffer
        {
            get
            {

                if (mIndexBuffer == null || CacheBuffers == false)
                {

                    mIndexBuffer = VividApp.GPU.Core.CreateIndexBuffer(Indices);

                }
            
                return mIndexBuffer;
         
            }

        }
        private vDataBuffer mIndexBuffer =null;
        public vDataBuffer VertexBuffer
        {
            get
            {
                if (mVertexBuffer == null || CacheBuffers == false)
                {
                    if(mVertexBuffer!=null){
                        mVertexBuffer.FreeData();

                    }
                    mVertexBuffer = VividApp.GPU.Core.CreateVertexBuffer(Vertices, UV, Normals);
                }
                return mVertexBuffer;
            }

        }
        private vDataBuffer mVertexBuffer=null;

    }

   

}
